package com.naizhuai.ui.msgmanager;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.ui.entity.management.ApplyID;
import com.naizhuai.ui.entity.management.RequestJoinMessageBean;
import com.naizhuai.util.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.subjects.PublishSubject;

/**
 * @author：lichenxi
 * @date 2018/6/14 14
 * email：525603977@qq.com
 * Fighting
 */
public class PersonExamineaAdapter extends RecyclerView.Adapter<PersonExamineaAdapter.ViewHolder> {

    private int selectedCount = 0;
    private PassRejectCallBack mPassRejectCallBack;

    public PersonExamineaAdapter(PassRejectCallBack passRejectCallBack) {
        mPassRejectCallBack = passRejectCallBack;
    }

    public int getSelectedCount() {
        return selectedCount;
    }

    private void notifySelected(boolean increase) {
        selectedCount += increase ? 1 : -1;
    }

    List<RequestJoinMessageBean> result;

    PublishSubject<String> checkSubject = PublishSubject.create();

    public void setResult(List<RequestJoinMessageBean> result) {
         this.result = result;
    }

    public PublishSubject<String> getCheckSubject() {
        return checkSubject;
    }

    public void changeSelect(boolean state) {
        if (state) {
            selectedCount = result.size();
        } else {
            selectedCount = 0;
        }
        for (RequestJoinMessageBean messageBean : result) {
            messageBean.setSelected(state);
        }
        notifyDataSetChanged();
    }

    public ArrayList<ApplyID> getSelected() {
        if (result != null) {
            ArrayList<ApplyID> arrayList = new ArrayList<>(selectedCount);
            for (RequestJoinMessageBean messageBean : result) {
                if (messageBean.isSelected()) {
                    arrayList.add(new ApplyID(messageBean.getReplayId()));
                }
            }
            return arrayList;
        }
        return null;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_person_examine_layout, parent, false);
        return new ViewHolder(view);
    }

    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //各种判断 点击进入对应的 界面
        RequestJoinMessageBean joinMessageBean = result.get(position);
        Context context=holder.itemView.getContext();
        holder.namePhone.setText(context.getString(R.string.name_phone,joinMessageBean.getUsername(),joinMessageBean.getPhoneNumber()));
        holder.requestTitle.setText(joinMessageBean.getRequestMessage());
        holder.time.setText(TimeUtils.millis2String(Long.parseLong(joinMessageBean.getRequestTime())*1000));

        holder.chosenSelect.setSelected(joinMessageBean.isSelected());
        holder.chosenSelect.setOnClickListener(v -> {
            notifySelected(joinMessageBean.toggleSelected());
            holder.chosenSelect.setSelected(joinMessageBean.isSelected());
            checkSubject.onNext("");
        });
        holder.pass.setOnClickListener(v -> {
            if (mPassRejectCallBack != null) {
                mPassRejectCallBack.pass(joinMessageBean.getReplayId());
            }
        });
        holder.reject.setOnClickListener(v -> {
            if (mPassRejectCallBack != null) {
                mPassRejectCallBack.reject(joinMessageBean.getReplayId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return result != null ? result.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.chosen_select)
        ImageView chosenSelect;
        @BindView(R.id.pass)
        TextView pass;
        @BindView(R.id.reject)
        TextView reject;
        @BindView(R.id.name_phone)
        TextView namePhone;
        @BindView(R.id.request_title)
        TextView requestTitle;
        @BindView(R.id.time)
        TextView time;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    public interface PassRejectCallBack {
        void pass(String applyId);
        void reject(String applyId);

    }
}
